/*
 * @Author: 胡海
 * @Date: 2020-02-21 18:23:26
 * @LastEditors: 胡海
 * @LastEditTime: 2020-03-07 21:17:44
 * @Description: 
 */
const {
    sequelize
} = require('../../core/db.js')


const {
    Sequelize,
    Model,
    Op
} = require('sequelize')

// define
class BuyGoods extends Model {
    // 业务表
    static async goodsList(obj) {
        const total = await BuyGoods.count()
        const list = await BuyGoods.findAll({
            where: {
                goodsName: {
                    [Op.like]: `%${obj.p}%`
                },
                // type: {
                //     [Op.like]: `%${obj.type}%`
                // },
            },
            order: [
                ['id']
            ],
            limit: obj.count,
            offset: (obj.start - 1) * obj.count,
        })
        const data = {
            list,
            total
        }
        return data
    }
    static async addGoods(data) {
        const info = await BuyGoods.findOne({
            where: {
                coding: data.coding
            }
        })
        if (info) {
            throw new global.errs.LikeError('产品已存在')
        }
        return await BuyGoods.create(data)
    }
    static async delGoods(data) {
        const info = await BuyGoods.destroy({
            where: {
                id: data.id
            }
        })
    }
    static async editGoods(data) {
        let {id,...all} = data
        const info = await BuyGoods.update(all, {
            where: {
                id: data.id
            }
        })
    }
}

BuyGoods.init({
    orderPrice: Sequelize.INTEGER,
}, {
    sequelize,
    tableName: 'product',
    alter: true
})

module.exports = {
    BuyGoods
}

// 数据迁移 SQL 更新 风险